package sg.edu.nus.dbd.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import sg.edu.nus.dbd.biz.ConfigurationBiz;
import sg.edu.nus.dbd.formbean.ThresholdConfig;
import sg.edu.nus.dbd.util.MyLogger;

/**
 * 
 * @author Ashok Savarimuthu
 * 
 */
public class ThresholdConfigAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		try {
			MyLogger.debug(this.getClass(), "list::Start...");
			ConfigurationBiz configCtlr = new ConfigurationBiz();
			List<ThresholdConfig> list = configCtlr.getThresholdParameters();
			req.setAttribute("ConfigList", list);
			return mapping.findForward("listingPage");
		} catch (Exception e) {
			MyLogger.error(this.getClass(), e);
			return mapping.findForward("error");
		}
	}

	public ActionForward listThParam(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		return mapping.findForward("list");

	}

	public ActionForward editThParam(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		try {
			String thParamId = req.getParameter("thParamId");

			MyLogger.debug(this.getClass(), "editThParam::Start...");
			ConfigurationBiz configCtlr = new ConfigurationBiz();
			ThresholdConfig thConfig = configCtlr.getThresholdParameter(Integer
					.parseInt(thParamId));
			System.out.println("The min: " + thConfig.getMinLevel());
			System.out.println("The max: " + thConfig.getMaxLevel());
			System.out.println("The warning: " + thConfig.getWarningLevel());
			System.out.println("The critical: " + thConfig.getCriticalLevel());
			req.setAttribute("Config", thConfig);
			return mapping.findForward("updatePage");
		} catch (Exception e) {
			MyLogger.error(this.getClass(), e);
			return mapping.findForward("error");
		}
	}

	public ActionForward saveThParam(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		try {
			MyLogger.debug(this.getClass(), "saveThParam::Start...");
			ThresholdConfig config = new ThresholdConfig();
			config.setId(Integer.parseInt(req.getParameter("thParamId")));
			config.setName(req.getParameter("name"));
			config
					.setMinLevel(Double.parseDouble(req
							.getParameter("minLevel")));
			config
					.setMaxLevel(Double.parseDouble(req
							.getParameter("maxLevel")));
			config.setCriticalLevel(Double.parseDouble(req
					.getParameter("criticalLevel")));
			config.setWarningLevel(Double.parseDouble(req
					.getParameter("warningLevel")));
			ConfigurationBiz configCtlr = new ConfigurationBiz();
			boolean success = configCtlr.updateThresholdConfig(config);
			if (success)
				return mapping.findForward("list");
			else {
				req.setAttribute("Config", config);
				return mapping.findForward("updatePage");
			}
		} catch (Exception e) {
			MyLogger.error(this.getClass(), e);
			return mapping.findForward("error");
		}
	}
}
